const app = getApp()

const defaultAvatarUrl = 'https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/avatarUrl.png'
Page({
  data: {
    avatarUrl: defaultAvatarUrl,
    theme: wx.getSystemInfoSync().theme,
    value:''
  },
  onLoad() {
    wx.onThemeChange((result) => {
      this.setData({
        theme: result.theme
      })
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    this.setData({
      avatarUrl,
    })
    this.login()
  },
  bindinput(e){
    this.setData({
      value:e.detail.value
    })
  },
  bindnicknamereview(e){
    console.log(e)
  },
  bindblur(e){
    this.setData({
      value:e.detail.value
    })
  },
  // 画布转图片
  draw:function(){
    
  },
  login:function(){
    let that =this
    let id =  app.globalData.xCustomer._openid
    wx.cloud.uploadFile({
      cloudPath: 'profile/' + id + "/" + new Date().getTime() + '.png',
      filePath: that.data.avatarUrl
    }).then( xRes => {
      wx.cloud.callFunction({
        name: "user",
        data: {
          action: "login",
          userInfo: {
            avatarUrl:xRes.fileID,
            nickName:that.data.value
          },
        },
        success: function (res) {
          wx.showToast({
            title: '登录成功',
            icon:'none'
          })
        }
      })
    })
  }
})
